package cn.xuchunh.campusbbs.ui.fragments;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.adapter.PhotoListAdapter;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.base.BaseRefreshListFragment;
import cn.xuchunh.campusbbs.bean.Picture;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.ui.activities.ImageActivity;
import cn.xuchunh.campusbbs.utils.Constants;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;
import cn.xuchunh.campusbbs.utils.JsonKey;
import cn.xuchunh.campusbbs.utils.JsonParser;
import cn.xuchunh.campusbbs.viewholder.AlbumViewHolder;
import cn.xuchunh.campusbbs.viewholder.PhotoViewHolder;

/**
 * Created by XuChunH on 2015/10/22.
 */
public class PhotoListFragment extends BaseRefreshListFragment<PhotoViewHolder, Picture> {

    private int albumId;
    private int userId;

    public static PhotoListFragment newInstance(int albumId, int userId) {

        Bundle args = new Bundle();
        args.putInt(IntentParamsKey.ALBUM_ID, albumId);
        args.putInt(IntentParamsKey.USER_ID, userId);
        PhotoListFragment fragment = new PhotoListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new GridLayoutManager(getActivity(), Constants.PHOTO_COLUMN_COUNT, LinearLayoutManager.VERTICAL, false);
    }

    @Override
    protected BaseRecyclerViewAdapter<PhotoViewHolder, Picture> createAdapter() {
        return new PhotoListAdapter(getActivity());
    }

    @Override
    protected List<Picture> parse(JSONObject jsonObject) throws JSONException {
        List<Picture> list = new ArrayList<>();
        JSONArray jsonArray = jsonObject.optJSONArray(JsonKey.LIST);
        JSONObject pictureJson;
        if (jsonArray != null) {
            int size = jsonArray.length();
            for (int i = 0; i < size; i++) {
                pictureJson = jsonArray.optJSONObject(i);
                if (pictureJson != null) {
                    Picture picture = new Picture();
                    picture.setId(pictureJson.optInt(JsonKey.PIC_ID));
                    picture.setTitle(pictureJson.optString(JsonKey.TITLE));
                    picture.setUserId(pictureJson.optInt(JsonKey.USER_ID));
                    picture.setReleaseDate(JsonParser.parseLong(pictureJson, JsonKey.RELEASE_DATE));
                    picture.setLastUpdateDate(JsonParser.parseLong(pictureJson, JsonKey.LAST_UPDATE_DATE));
                    picture.setUserName(pictureJson.optString(JsonKey.USER_NICK_NAME));
                    picture.setHot(pictureJson.optInt(JsonKey.HOT));
                    picture.setReplies(pictureJson.optInt(JsonKey.REPLIES));
                    picture.setThumb(pictureJson.optString(JsonKey.THUMB_PIC));
                    picture.setSrc(pictureJson.optString(JsonKey.ORIGIN_PIC));
                    list.add(picture);
                }
            }
        }
        getAdapter().setHasMore(jsonObject.optInt(JsonKey.HAS_NEXT) != 0);
        return list;
    }

    @Override
    protected void loadDataFromServer() {
        HttpRequestManager.getInstance(getActivity()).loadUserPhotoList(
                userId,
                albumId,
                getPage(),
                30,
                new ListXJsonListener(),
                new SimpleErrorListener(),
                getVolleyTag()
        );
    }

    @Override
    protected void loadDataFromCache() {

    }

    @Override
    public String getVolleyTag() {
        return PhotoListFragment.class.getSimpleName();
    }

    @Override
    public String getCacheKey() {
        return null;
    }

    @Override
    protected void handleArgs(Bundle args) {
        super.handleArgs(args);
        if (args != null) {
            albumId = args.getInt(IntentParamsKey.ALBUM_ID, 0);
            userId = args.getInt(IntentParamsKey.USER_ID, 1);
        }
    }

    @Override
    public void onItemClick(int position, View item) {

        ImageActivity.launch(getActivity(), Uri.parse(getAdapter().getItem(position).getSrc()));

    }
}
